package com.j256.ormlite.stmt.mapped;

import com.j256.ormlite.dao.ObjectCache;
import com.j256.ormlite.db.DatabaseType;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.misc.SqlExceptionUtil;
import com.j256.ormlite.support.DatabaseConnection;
import com.j256.ormlite.table.TableInfo;
import java.sql.SQLException;

public class MappedUpdate<T, ID> extends BaseMappedStatement<T, ID>
{
  private final FieldType versionFieldType;
  private final int versionFieldTypeIndex;

  private MappedUpdate(TableInfo<T, ID> paramTableInfo, String paramString, FieldType[] paramArrayOfFieldType, FieldType paramFieldType, int paramInt)
  {
    super(paramTableInfo, paramString, paramArrayOfFieldType);
    this.versionFieldType = paramFieldType;
    this.versionFieldTypeIndex = paramInt;
  }

  public static <T, ID> MappedUpdate<T, ID> build(DatabaseType paramDatabaseType, TableInfo<T, ID> paramTableInfo)
    throws SQLException
  {
    FieldType localFieldType1 = paramTableInfo.getIdField();
    if (localFieldType1 == null)
      throw new SQLException("Cannot update " + paramTableInfo.getDataClass() + " because it doesn't have an id field");
    StringBuilder localStringBuilder = new StringBuilder(64);
    appendTableName(paramDatabaseType, localStringBuilder, "UPDATE ", paramTableInfo.getTableName());
    int i = 1;
    int j = 0;
    Object localObject = null;
    int k = -1;
    for (FieldType localFieldType3 : paramTableInfo.getFieldTypes())
      if (isFieldUpdatable(localFieldType3, localFieldType1))
      {
        if (localFieldType3.isVersion())
        {
          localObject = localFieldType3;
          k = j;
        }
        j++;
      }
    int i1 = j + 1;
    if (localObject != null)
      i1++;
    FieldType[] arrayOfFieldType2 = new FieldType[i1];
    FieldType[] arrayOfFieldType3 = paramTableInfo.getFieldTypes();
    int i2 = arrayOfFieldType3.length;
    int i3 = 0;
    int i4 = 0;
    while (i3 < i2)
    {
      FieldType localFieldType2 = arrayOfFieldType3[i3];
      int i6;
      if (!isFieldUpdatable(localFieldType2, localFieldType1))
      {
        i6 = i4;
        i3++;
        i4 = i6;
      }
      else
      {
        if (i != 0)
        {
          localStringBuilder.append("SET ");
          i = 0;
        }
        while (true)
        {
          appendFieldColumnName(paramDatabaseType, localStringBuilder, localFieldType2, null);
          i6 = i4 + 1;
          arrayOfFieldType2[i4] = localFieldType2;
          localStringBuilder.append("= ?");
          break;
          localStringBuilder.append(", ");
        }
      }
    }
    localStringBuilder.append(' ');
    appendWhereFieldEq(paramDatabaseType, localFieldType1, localStringBuilder, null);
    int i5 = i4 + 1;
    arrayOfFieldType2[i4] = localFieldType1;
    if (localObject != null)
    {
      localStringBuilder.append(" AND ");
      appendFieldColumnName(paramDatabaseType, localStringBuilder, localObject, null);
      localStringBuilder.append("= ?");
      (i5 + 1);
      arrayOfFieldType2[i5] = localObject;
    }
    return new MappedUpdate(paramTableInfo, localStringBuilder.toString(), arrayOfFieldType2, localObject, k);
  }

  private static boolean isFieldUpdatable(FieldType paramFieldType1, FieldType paramFieldType2)
  {
    return (paramFieldType1 != paramFieldType2) && (!paramFieldType1.isForeignCollection()) && (!paramFieldType1.isReadOnly());
  }

  public int update(DatabaseConnection paramDatabaseConnection, T paramT, ObjectCache paramObjectCache)
    throws SQLException
  {
    while (true)
    {
      int i;
      int k;
      try
      {
        if (this.argFieldTypes.length <= 1)
          return 0;
        Object[] arrayOfObject = getFieldObjects(paramT);
        FieldType localFieldType1 = this.versionFieldType;
        Object localObject1 = null;
        if (localFieldType1 != null)
        {
          Object localObject2 = this.versionFieldType.extractJavaFieldValue(paramT);
          localObject1 = this.versionFieldType.moveToNextValue(localObject2);
          arrayOfObject[this.versionFieldTypeIndex] = this.versionFieldType.convertJavaFieldToSqlArgValue(localObject1);
        }
        i = paramDatabaseConnection.update(this.statement, arrayOfObject, this.argFieldTypes);
        if (i > 0)
        {
          if (localObject1 != null)
            this.versionFieldType.assignField(paramT, localObject1, false, null);
          if (paramObjectCache != null)
          {
            Object localObject3 = this.idField.extractJavaFieldValue(paramT);
            Object localObject4 = paramObjectCache.get(this.clazz, localObject3);
            if ((localObject4 != null) && (localObject4 != paramT))
            {
              FieldType[] arrayOfFieldType = this.tableInfo.getFieldTypes();
              int j = arrayOfFieldType.length;
              k = 0;
              if (k < j)
              {
                FieldType localFieldType2 = arrayOfFieldType[k];
                if (localFieldType2 == this.idField)
                  break label290;
                localFieldType2.assignField(localObject4, localFieldType2.extractJavaFieldValue(paramT), false, paramObjectCache);
                break label290;
              }
            }
          }
        }
        logger.debug("update data with statement '{}' and {} args, changed {} rows", this.statement, Integer.valueOf(arrayOfObject.length), Integer.valueOf(i));
        if (arrayOfObject.length > 0)
        {
          logger.trace("update arguments: {}", arrayOfObject);
          return i;
        }
      }
      catch (SQLException localSQLException)
      {
        throw SqlExceptionUtil.create("Unable to run update stmt on object " + paramT + ": " + this.statement, localSQLException);
      }
      return i;
      label290: k++;
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.j256.ormlite.stmt.mapped.MappedUpdate
 * JD-Core Version:    0.6.2
 */